package com.nf.service;

import com.nf.entity.House;
import com.nf.entity.HouseInfo;
import com.nf.entity.Students;
import com.nf.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper=null;
    public Students test() {
        SqlSession session=null;

          try {
              InputStream inputStream= Resources.getResourceAsStream("mybatis-comfig.xml");
              SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
              session= sqlSessionFactory.openSession();
              studentMapper =session.getMapper(StudentMapper.class);
             // System.out.println("辣鸡实现类是谁："+studentMapper);
          } catch (IOException e) {
              e.printStackTrace();
              return null;
          }

        List<House> houseList = studentMapper.getAllHouseDetail();
        System.out.println(houseList.size());
        for (House h:houseList) {
            System.out.println(h.getId()+"  "+h.getName()+" 人数:"+h.getStudentsList().size());
            System.out.print("   学生明细：");
            for (Students s:h.getStudentsList()){
                System.out.print(s.getName());
            }
            System.out.println();
        }




       /* Map<String,Object> student = studentMapper.findStudentById(2);
        System.out.println(  student.get("dob") ) ;*/


        //session.commit();
        //List<StudentHouseInfo> infos = studentMapper.getAllStudentHouseInfo();

      /* List<HouseInfo> infos = studentMapper.getAllHouseInfo();
        for (HouseInfo hi:infos) {
            System.out.println(hi.getName()+" "+hi.getTcount()+" "+hi.getKzl());

            List<Students> students = studentMapper.findStudentByHouseId(hi.getHid());
            for (Students x:students){
                System.out.print(" "+x.getName());

            }
            System.out.println();
        }
        System.out.println(infos.size());*/


        //Map<String,Object> student = studentMapper.findStudentById(2);
        //System.out.println(  student.get("dob") ) ;

        //Student s = studentMapper.findAllStudents();
        //System.out.println(s.getStudId()+"  "+s.getName());
        /*
        List<Student> studentList = studentMapper.findAllStudents();
        for (Student s:studentList) {
            System.out.println(s.getStudId()+"  "+s.getName());
        }*/
        /*


        Student a = new Student();
        a.setStudId(3);
        a.setName("反波");
        a.setEmail("sss@qq.com");

        studentMapper.insertStudent(a);


        System.out.println(a.getStudId());

        student.setName("小雷new");
        studentMapper.updateStudent(student);
        */
        //session.commit();
        //List<StudentHouseInfo> infos = studentMapper.getAllStudentHouseInfo();

       /*
        List<HouseInfo> infos = studentMapper.getAllHouseInfo();
        for (HouseInfo hi:infos) {
            System.out.print(hi.getHid()+"   "+hi.getName()+" "+hi.getTcount()+" "+hi.getKzl());
            System.out.print("(");

            List<Student> students2 = hi.getStudentList();
            for (Student x : students2) {
                System.out.print(x.getName() + ",");
            }


            if (hi.getHid()==1) {

                List<Student> students = studentMapper.findStudentByHouseId(hi.getHid());
                for (Student x : students) {
                    System.out.print(x.getName() + ",");
                }
            }

            System.out.print(")");
            System.out.println();


        } */
        return null;
    }
}
